﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Json;

namespace ToodledoAPI.Exceptions
{
    public abstract class ToodledoException:Exception
    {
        public ToodledoException(string JsonReturn)
        {
            JsonObject jsonError = (JsonObject)JsonObject.Parse(JsonReturn);
            errorCode = jsonError["errorCode"];
            errorDesc = jsonError["errorDesc"];
        }

        public int errorCode { get; set; }
        public string errorDesc { get; set; }
        
        public abstract override string Message
        { get; }

        protected string UnknownMessage
        {
            get
            {
                switch (errorCode)
                {
                    case 1:
                        return "You did not specify a key for authentication";
                    case 2:
                        return "The authentication key that you provided has expired or is invalid.";
                    case 100:
                        return "UnknownEror";
                    case 400:
                        return "This app has been blocked by the user";
                    case 500:
                        return "The Toodledo server is offline for maintenance";
                    default:
                        return errorDesc;
                }
            }
        }
    }

    public static class ToodledoExceptionTester
    {
        public static bool IsToodledoException(this string s)
        {
            JsonObject jsonError = JsonObject.Parse(s) as JsonObject;
            if (jsonError != null)
            {
                return jsonError.Keys.Contains("errorCode") && jsonError.Keys.Contains("errorDesc");
            }
            else
            {
                return false;
            }
        }
    }
}
